package cn.ycxy.lbl.common.restful;

import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author BaoL
 * @Date 2023/3/11 20:49
 * @Description 统一返回结果类
 * @Since version-1.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class R<T> {

    private Integer code;

    private String message;

    private T data;

    /**
     * 构造方法私有化
     */
    private R(){}

    /**
     * 返回成功结果
     * @return
     */
    public static<T> R<T> ok(T data){
        R<T> r = new R<>();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }
    public static R<Void> ok() {
        return ok(null);
    }

    /**
     * 返回错误结果
     * @return
     */
    public static R<Void> error(){
        R<Void> r = new R<>();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMessage(ResponseEnum.ERROR.getMessage());
        return r;
    }

    /**
     * 设置特定的结果
     * @param re
     * @return
     */
    public static R<Void> setResult(ResponseEnum re){
        R<Void> r = new R<>();
        r.setCode(re.getCode());
        r.setMessage(re.getMessage());
        return r;
    }


    /**
     * 设置特定的消息
     * @param msg
     * @return
     */
    public R<T> msg(String msg){
        this.setMessage(msg);
        return this;
    }

}
